<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手写 Promise</title>
</head>
<body>

    <!-- <script src="./promise.js"></script> -->
    <script src="./promise_class.js"></script>

    <!-- 测试 MyPromise 构造函数 -->
    <!-- <script>
        const promise = new MyPromise((resolve, reject) => {
            setTimeout(() => {
                resolve(1)
                // reject(2)
            }, 1000)
        })

        promise.then(value => {
            console.log('onResolved1:', value)
        }, reason => {
            console.log('onRejected1:', reason)
        })

        promise.then(value => {
            console.log('onResolved2:', value)
        }, reason => {
            console.log('onRejected2:', reason)
        })
    </script> -->

    <!-- 测试 then 方法 -->
    <!-- <script>
        new MyPromise((resolve, reject) => {
            setTimeout(() => {
                resolve(1)
                // reject(2)
            }, 1000)
        })
        .then(value => {
            console.log('value1:', value)
            throw value
        }, reason => {
            console.log('reason1:', reason)
            return reason
        })
        .then(value => {
            console.log('value2:', value)
        }, reason => {
            console.log('reason2:', reason)
            throw reason
        })
        .catch(error => {
            console.log('error:', error)
            // return error
            return new MyPromise(() => {})
        })
        .then(value => {
            console.log('value3:', value)
        }, reason => {
            console.log('reason3:', reason)
        })
    </script> -->

    <!-- 测试 resolve reject 方法 -->
    <!-- <script>
        const p1 = MyPromise.resolve(1)
        const p2 = MyPromise.resolve(MyPromise.resolve(2))
        const p3 = MyPromise.resolve(MyPromise.reject(3))
        p1.then(value => {
            console.log('value1:', value)
        })
        p2.then(value => {
            console.log('value2:', value)
        })
        p3.catch(error => {
            console.log('error:', error)
        })
    </script> -->

    <!-- 测试 all race 方法 -->
    <!-- <script>
        const p1 = MyPromise.resolve(1)
        const p2 = MyPromise.resolve(MyPromise.resolve(2))
        const p3 = MyPromise.resolve(MyPromise.reject(3))
        const p4 = new MyPromise((resolve, reject) => {
            setTimeout(() => {
                resolve(4)
            }, 1000)
        })

        // const pAll = MyPromise.all([p4, p1, p2])
        // pAll.then(value => {
        //     console.log('value:', value)
        // }, reason => {
        //     console.log('reason:', reason)
        // })

        const pRace = MyPromise.race([p4, 6, p1, p2, p3])
        pRace.then(value => {
            console.log('value:', value)
        }, reason => {
            console.log('reason:', reason)
        })
    </script> -->

    <!-- 测试 resolveDelay rejectDelay 方法 -->
    <script>
        const p1 = MyPromise.resolveDelay(1, 1000)
        const p2 = MyPromise.rejectDelay(2, 2000)
        
        p1.then(value => {
            console.log('value:', value)
        })
        p2.catch(error => {
            console.log('error:', error)
        })
    </script>

</body>
</html>